package com.naihe.anderson.sms.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

public class AsyncImageLoader {

	public int DISPLAY_WIDTH = 200;
	public int DISPLAY_HEIGHT = 300;

	public AsyncImageLoader(int width, int height) {
		this.DISPLAY_HEIGHT = height;
		this.DISPLAY_WIDTH = width;
	}

	public interface ImageCallback {
		public void imageLoaded(Bitmap obj);
	}

	public void loadImageFromUrl(final String imageUrl, final String mapPicNmae, final ImageCallback callback) {
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				callback.imageLoaded((Bitmap) msg.obj);
			}
		};

		new Thread() {
			public void run() {
				try {
					boolean isSaveToLocal = false;
					final Bitmap bitmap;
					if (TextUtils.isEmpty(FileUtil.getPicNotePath())) {
						bitmap = BitmapFactory.decodeStream(
								new URL(imageUrl.replace("url_thumb_", "")).openStream(),
								null,
								getOptionsConfig(null,new URL(imageUrl.replace("url_thumb_", ""))));
					} else {
						File mapfis = new File(FileUtil.getPicNotePath() + mapPicNmae);
						if(mapfis.exists()){
							bitmap = getLoacalBitmap(mapPicNmae);
						} else {
							bitmap = BitmapFactory.decodeStream(
									new URL(imageUrl.replace("url_thumb_", "")).openStream(),
									null,
									getOptionsConfig(null,new URL(imageUrl.replace("url_thumb_", ""))));
							isSaveToLocal = true;
						}
					}
					handler.sendMessage(handler.obtainMessage(0, bitmap));

					if (isSaveToLocal) {
						new Thread() {
							public void run() {
								try {
									Bitmap temp = ImageUtil.resizeBitmap( bitmap, DISPLAY_WIDTH, DISPLAY_HEIGHT);
									ImageUtil.bitmapSava(temp, FileUtil.getPicNotePath() + mapPicNmae, Bitmap.CompressFormat.PNG);
								} catch (Exception e) {
									FileUtil.addLog("", AsyncImageLoader.class, "AsyncImageLoader", e);
								}
							}
						}.start();
					}
				} catch (Exception e) {
					FileUtil.addLog("", AsyncImageLoader.class, "AsyncImageLoader", e);
				}
			}
		}.start();
	}

	public BitmapFactory.Options getOptionsConfig(String fileName, URL url)
			throws Exception {
		BitmapFactory.Options op = new BitmapFactory.Options();
		op.inJustDecodeBounds = true;
		if (url != null) {
			BitmapFactory.decodeStream(url.openStream(), null, op); // 锟斤拷取锟竭达拷锟斤拷息
		} else
			BitmapFactory.decodeFile(fileName, op);
		int wRatio = (int) Math.ceil(op.outWidth / DISPLAY_WIDTH);
		int hRatio = (int) Math.ceil(op.outHeight / DISPLAY_HEIGHT);
		if (wRatio > 1 && hRatio > 1) {
			if (wRatio > hRatio) {
				op.inSampleSize = wRatio;
			} else {
				op.inSampleSize = hRatio;
			}
		} else if (op.outWidth == -1) {
			op.inSampleSize = 20;
		}
		op.inJustDecodeBounds = false;
		return op;
	}

	public Bitmap getLoacalBitmap(String draw_name) throws Exception{
		try {
			FileInputStream fis = new FileInputStream(FileUtil.getPicNotePath() + draw_name);
			return BitmapFactory.decodeStream(fis, null, getOptionsConfig(draw_name, null));
		} catch (FileNotFoundException e) {
			FileUtil.addLog("", AsyncImageLoader.class, "AsyncImageLoader", e);
			return null;
		}
	}
}
